using AppToolKit.Data.NHibernate.i18;
using Common.Logging;
using NHibernate;
using NHibernate.AdoNet;
using Spring.Data.NHibernate;
using Spring.Data.NHibernate.Generic.Support;
using System;
using System.Collections.Generic;
namespace AppToolKit.Data.NHibernate
{
	public abstract class DataAccessObjectBase<T> : HibernateDaoSupport, IDataAccessObject<T> where T : class
	{
		private static readonly ILog logger_ = LogManager.GetLogger(typeof(DataAccessObjectBase<T>));
		private IDbProcessor native_;
		public IDbProcessor NativeDatabase
		{
			get
			{
				return this.native_;
			}
			set
			{
				this.native_ = value;
			}
		}
		public virtual void Save(T t)
		{
			try
			{
				base.HibernateTemplate.Save(t);
				base.Session.Flush();
			}
			catch (Exception ex)
			{
				DataAccessObjectBase<T>.ExceptionHandle(ex);
			}
			finally
			{
				base.Session.Clear();
			}
		}
		private static void ExceptionHandle(Exception ex)
		{
			DataAccessObjectBase<T>.logger_.Error(string.Format("{0}", ex));
			if (ex.GetBaseException() is TooManyRowsAffectedException)
			{
				throw new NoRollbackException();
			}
			if (ex.GetBaseException() is StaleStateException)
			{
				throw new DatabaseException(StringResource.DataAccessObjectBase_ExceptionHandle_Dirty);
			}
			throw ex;
		}
		public virtual void Delete(T entity)
		{
			try
			{
				base.HibernateTemplate.Delete(entity);
				base.Session.Flush();
			}
			catch (Exception ex)
			{
				DataAccessObjectBase<T>.ExceptionHandle(ex);
			}
			finally
			{
				base.Session.Clear();
			}
		}
		public virtual void Update(T t)
		{
			try
			{
				DataAccessObjectBase<T>.logger_.Debug("正在更新数据");
				base.HibernateTemplate.Update(t);
				base.Session.Flush();
				DataAccessObjectBase<T>.logger_.Debug(string.Format("更新数据完成", new object[0]));
			}
			catch (Exception ex)
			{
				DataAccessObjectBase<T>.ExceptionHandle(ex);
			}
			finally
			{
				base.Session.Clear();
			}
		}
		public virtual bool SaveOrUpdate(T t)
		{
			base.HibernateTemplate.SaveOrUpdate(t);
			return true;
		}
		public virtual IList<T> GetAll()
		{
			return base.HibernateTemplate.LoadAll<T>();
		}
		public IList<T> GetList(string query)
		{
			ISession session = base.Session;
			IList<T> result = session.CreateQuery(query).List<T>();
			base.Session.Clear();
			return result;
		}
		public virtual T Load(object t)
		{
			T result;
			try
			{
				T t2 = base.HibernateTemplate.Load<T>(t);
				base.Session.Clear();
				result = t2;
			}
			catch (HibernateObjectRetrievalFailureException ex)
			{
				if (!(ex.InnerException is ObjectNotFoundException))
				{
					throw;
				}
				result = default(T);
			}
			return result;
		}
		public IList<T> GetListByPage(string query, int start, int Max)
		{
			ISession session = base.Session;
			IList<T> result = session.CreateQuery(query).SetFirstResult(start).SetMaxResults(Max).List<T>();
			base.Session.Clear();
			return result;
		}
		public void Refresh(T data)
		{
			base.HibernateTemplate.Refresh(data);
		}
		public T Get(object key)
		{
			return base.Session.Get<T>(key);
		}
		public void Load(T data, object key)
		{
			try
			{
				base.HibernateTemplate.Load(data, key);
				base.Session.Clear();
			}
			catch (HibernateObjectRetrievalFailureException ex)
			{
				if (!(ex.InnerException is ObjectNotFoundException))
				{
					throw;
				}
				data = default(T);
			}
		}
		public virtual void Delete(string queryString)
		{
			base.HibernateTemplate.Delete(queryString);
		}
	}
}
